package com.mipo.client.javase.mongodb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;


public class MongoUtilWeihelei {
    	public static void main(String[] args) throws Exception {
    		//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
    		//ServerAddress()两个参数分别为 服务器地址 和 端口
    		ServerAddress serverAddress = new ServerAddress("120.55.57.60",27017);
    		List<ServerAddress> addrs = new ArrayList<ServerAddress>();
    		addrs.add(serverAddress);
    		MongoCredential credential = MongoCredential.createMongoCRCredential("", "third_swap_data", "".toCharArray());
    		List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
    		credentials.add(credential);
    		//通过连接认证获取MongoDB连接  
    		MongoClient mongoClient = new MongoClient(addrs,credentials);
    		
    		MongoClient mongoClient2 = new MongoClient("120.55.57.60", 27017);
    		MongoDatabase db = mongoClient2.getDatabase("third_swap_data");
    		MongoCollection<Document> users = db.getCollection("usr");
    		FindIterable<Document> documents =users.find().skip(0).limit(100);
    		for (Document document : documents) {
    			System.out.println(document); //Document{{_id=5ae171ba15a9a37e84d39e31, name=tompig}}
    		}
    		
		}
    	
}